﻿Public Class frmStatPrdQty
    Dim dsStatQty As DataSet
    Dim daStatQty As OleDb.OleDbDataAdapter
    Dim sql As String

    Private Sub frmStatPrdQty_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub frmStatPrdQty_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)
        Call dbConnection()
        Call gSubMkCmbYear(cmbYear)
        Call mkCmbProduct()
        cmbInOut.SelectedIndex = 0
        Call grid1Refresh("default")
    End Sub

    Private Sub btnGoStatistics_Click(sender As System.Object, e As System.EventArgs) Handles btnGoStatistics.Click
        Dim result As Integer = MessageBox.Show("제품 입출고 현황을 지금 조사 하시겠습니까?", "입출고 현황 조사", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            Exit Sub
        ElseIf result = DialogResult.Yes Then
            btnSearch.Enabled = False
            btnSearchReset.Enabled = False
            Call gSubLookupProdQty()
            MsgBox("매입매출 계정별 현황 조사가 완료 되었습니다")
            btnSearch.Enabled = True
            btnSearchReset.Enabled = True
        End If
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Call grid1Refresh("")
    End Sub

    Private Sub btnNewWindow_Click(sender As System.Object, e As System.EventArgs) Handles btnNewWindow.Click
        Dim MDIFormStatQty As New frmStatPrdQty
        MDIFormStatQty.Show()
    End Sub

    Private Sub cmbInOut_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbInOut.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbProduct_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbProduct.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbYear_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbYear.KeyPress
        e.Handled = True
    End Sub

#Region "프로시져"

    Private Sub grid1Refresh(ByVal initFlag As String)
        Call dbConnection()
        Dim addSql As String = ""
        Dim valFindYear As String = ""
        Dim valFindPrdCd As String = ""
        Dim totalAmt As Double = 0
        Dim totalOrgAmt As Double = 0
        Dim totalRestAmt As Double = 0

        If cmbInOut.SelectedIndex = 0 Then
            addSql = addSql + " AND A.inOut='IN' "
        Else
            addSql = addSql + " AND A.inOut='OT' "
        End If
        If cmbYear.SelectedValue.ToString <> "-전체-" Then
            valFindYear = cmbYear.SelectedValue.ToString
        End If
        If valFindYear <> "" Then
            addSql = addSql + " AND A.Yr='" + valFindYear + "' "
        End If
        If cmbProduct.SelectedValue.ToString <> "0" Then
            valFindPrdCd = cmbProduct.SelectedValue.ToString
        End If
        If valFindPrdCd <> "" Then
            addSql = addSql + " AND A.typeCd=" + valFindPrdCd + " "
        End If

        If initFlag = "default" Then
            addSql = " AND A.flag='N' "
        End If
        dsStatQty = New DataSet

        sql = "SELECT A.Yr as 년도, B.PrdNm as 계정, A.m01 as 1월, A.m02 as 2월, A.m03 as 3월, A.m04 as 4월, A.m05 as 5월, A.m06 as 6월, A.m07 as 7월, A.m08 as 8월, A.m09 as 9월, A.m10 as 10월, A.m11 as 11월, A.m12 as 12월, A.seq, A.issueDt as 조사일" & _
            "       FROM tblStatProduct A LEFT JOIN tblProduct B ON A.prdCd=B.prdCd " + _
            "       WHERE A.flag='Y' AND A.types='QTY' " + addSql + _
            "    ORDER BY A.Yr, B.PrdNm DESC "

        Try
            daStatQty = New OleDb.OleDbDataAdapter(sql, dbConn)
            daStatQty.Fill(dsStatQty, "StatList")

            grdStatQty.DataSource = dsStatQty.Tables("StatList")
            lbGrid1Cnt.Text = "총 " + dsStatQty.Tables("StatList").Rows.Count.ToString + " 건"

            dbConn.Close()
            Call setGrid1Design()

            grdStatQty.Columns(1).Frozen = True
        Catch err As ObjectDisposedException
            dbConn.Close()
            MsgBox(sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub

    Private Sub setGrid1Design()

        grdStatQty.Columns(0).Width = "60"
        grdStatQty.Columns(1).Width = "150"

        grdStatQty.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(2).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(2).Width = "90"
        grdStatQty.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(3).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(3).Width = "90"
        grdStatQty.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(4).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(4).Width = "90"
        grdStatQty.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(5).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(5).Width = "90"
        grdStatQty.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(6).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(6).Width = "90"
        grdStatQty.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(7).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(7).Width = "90"

        grdStatQty.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(8).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(8).Width = "90"
        grdStatQty.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(9).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(9).Width = "90"
        grdStatQty.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(10).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(10).Width = "90"
        grdStatQty.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(11).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(11).Width = "90"
        grdStatQty.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(12).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(12).Width = "90"
        grdStatQty.Columns(13).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatQty.Columns(13).DefaultCellStyle.Format = "n0"
        grdStatQty.Columns(13).Width = "90"
        grdStatQty.Columns(14).Visible = False
        grdStatQty.Columns(15).Width = "150"

        With grdStatQty.DefaultCellStyle
            .Alignment = DataGridViewContentAlignment.MiddleLeft
            If cmbInOut.SelectedIndex = 0 Then
                .ForeColor = Color.Blue
            Else
                .ForeColor = Color.Red
            End If
        End With

        grdStatQty.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdStatQty.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdStatQty.Columns(0).DefaultCellStyle.ForeColor = Color.Black
        grdStatQty.Columns(1).DefaultCellStyle.ForeColor = Color.Black

        'For i = 0 To grdStatQty.Rows.Count - 1
        '    If cmbInOut.SelectedIndex = 0 Then
        '        grdStatQty.Rows(i).DefaultCellStyle.ForeColor = Color.Blue
        '    Else
        '        grdStatQty.Rows(i).DefaultCellStyle.ForeColor = Color.Red
        '    End If
        'Next
        'grdStatQty.Columns(2).DefaultCellStyle.ForeColor = Color.Black

    End Sub
    '## 제품 정보 가져오기
    Private Sub mkCmbProduct()
        Dim sql As String
        Dim dsStatQty As DataSet
        Dim daStatQty As OleDb.OleDbDataAdapter
        Dim addSql As String = ""
        sql = "SELECT prdCd, prdNm FROM tblProduct WHERE flag='Y' ORDER BY prdNm"
        dsStatQty = New DataSet
        daStatQty = New OleDb.OleDbDataAdapter(sql, dbConn)
        daStatQty.Fill(dsStatQty, "cmbPrd")

        dbConn.Close()
        Dim newDsRow As DataRow = dsStatQty.Tables("cmbPrd").NewRow
        newDsRow("prdNm") = "-전체-"
        newDsRow("prdCd") = "9999"

        dsStatQty.Tables("cmbPrd").Rows.Add(newDsRow)

        newDsRow = dsStatQty.Tables("cmbPrd").NewRow
        newDsRow("prdNm") = "-제품-"
        newDsRow("prdCd") = "0"

        dsStatQty.Tables("cmbPrd").Rows.Add(newDsRow)
        With cmbProduct
            .DataSource = dsStatQty.Tables("cmbPrd")
            .DisplayMember = "prdNm"
            .ValueMember = "prdCd"
        End With
        cmbProduct.SelectedIndex = dsStatQty.Tables("cmbPrd").Rows.Count - 1
    End Sub
#End Region

End Class